﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCControl;

namespace Cost.Controllers
{
    public class DishSignController : Controller
    {
        private readonly ICostRepository<DishDelivery> dishdeliveryRepository;
        private readonly ICostRepository<DishDeliveryItem> dishdeliveryitemRepository;
        private readonly ICostRepository<StockRecord> stockrecordRepository;

        public DishSignController(ICostRepository<DishDelivery> dishdeliveryRepository,
            ICostRepository<DishDeliveryItem> dishdeliveryitemRepository,
            ICostRepository<StockRecord> stockrecordRepository)
        {
            this.dishdeliveryRepository = dishdeliveryRepository;
            this.dishdeliveryitemRepository = dishdeliveryitemRepository;
            this.stockrecordRepository = stockrecordRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = dishdeliveryRepository.GetPageable(pager, m => m.State == "Delivery", where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult Edit(string Id)
        {
            var data = dishdeliveryRepository.Find(Id) ?? new DishDelivery();
            if (string.IsNullOrEmpty(data.CreateUserId))
            {
                data.Date = DateTime.Now;
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(string Id, IList<DishDeliveryItem> MaterialList)
        {
            var oldData = dishdeliveryRepository.Find(Id);
            oldData.State = "Sign";
            dishdeliveryRepository.Save(oldData);
            foreach (var childitem in MaterialList)
            {
                var childdata = dishdeliveryitemRepository.Find(childitem.Id);
                childdata.SignCount = childitem.SignCount;
                childdata.State = "Sign";
                dishdeliveryitemRepository.Save(childdata);
                //保存库存记录
                StockRecord StockRecorddate = new StockRecord()
                {
                    Id = Guid.NewGuid().ToString(),
                    MaterialId = childitem.MaterialId,
                    Type = "物流签收",
                    Count = childitem.SignCount,
                };
                stockrecordRepository.Insert(StockRecorddate);
            }
            return Json(AjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult Delete(string id)
        {
            dishdeliveryRepository.Delete(id);
            dishdeliveryitemRepository.DeleteAll(m => m.OrderId == id);
            return Json(AjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult MaterialList(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = dishdeliveryRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult EditList()
        {
            return View();
        }

        [HttpPost]
        public JsonResult EditList(string parentId, nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = dishdeliveryitemRepository.GetPageable(pager, d => d.OrderId == parentId, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
    }
}
